{-
	myIO Module
	
	r51
	
	29 March 2010
	
	Daniel De Guzman
	
	This module is part of the FPT Algorithm Implementation Project for the Massey University
	paper: 159.402 Programming Languages. It contains functions that ascertain input file's 
	validity.
-}

module MyIO where

import Datatypes
import Words
import Char
import List

-- |=========================================================================================|
-- |									Boolean Functions									 |
-- |=========================================================================================|

-- Check if the specified string is in the alphabet
areAlpha :: String -> Bool
areAlpha [] = False
areAlpha x = and $ map isAlpha x

-- Check if the specified string is a number
areDigits :: String -> Bool
areDigits [] = False
areDigits x = and $ map isDigit x

-- Check if the specified string is a comma
isComma :: String -> Bool
isComma [] = False
isComma c = if c == ","
			then True
			else False

-- Check's the file extension. Will continue only if it is RTF
checkExtension :: FilePath -> Bool
checkExtension f 
	| (getExtension f == "rtf") = True
	| (getExtension f == "txt")	= True
	| otherwise 				= False

-- Checks each graph information line for format
--checkLines :: GraphInfo -> Bool
checkLines list = and $ map checkLine list

-- Checks a line for format
checkLine :: String -> Bool
checkLine list = checkLabel $ getWord list

-- Checks the vertex label and comma
checkLabel :: String -> Bool
checkLabel list 
	= 	(isComma $ take 1 $ reverse list) 
--	&& 	(areAlpha $ drop 1 $ reverse list)

-- |-----------------------------------------------------------------------------------------|

-- |=========================================================================================|
-- | 							Information Acquisition Functions							 |
-- |=========================================================================================|
-- Gets the input file's extension
getExtension :: FilePath -> FileExt
getExtension f = reverse $ take 3 $ reverse f

-- Gets the parameter k from the file
getK :: FileExt -> Contents -> K
getK ext k 
	| (map toLower ext == "txt") = getWord k
	| (map toLower ext == "rtf") = getWord $ drop 314 k


-- Gets the vertex count from the file
getVertexCount :: FileExt -> Contents -> VertexCount
getVertexCount ext contents 
	| (map toLower ext == "txt") = getWord $ drop 1 $ dropWord contents
	| (map toLower ext == "rtf") = getWord $ drop 2 $ dropWord $ drop 314 contents
		
-- Gets the vertex information and removes unnecessary info
-- List is reversed to remove '}' at the end of the file
-- Reversed again to recover original file structure
getVertexInfo :: FileExt -> Contents -> GraphInfo
getVertexInfo ext vi 
	| (map toLower ext == "txt") = splitWords2 $ drop 1 $ dropWord $ drop 1 $ dropWord vi
	| (map toLower ext == "rtf") = splitWords2 $ reverse $ drop 1 $ reverse $ drop 321 vi

-- |-----------------------------------------------------------------------------------------|
-- |=========================================================================================|